// Console Firebug Hack : turn off console for your beta and dev versions.
// @author  Jacksay
console = console || {
  info: function(){},
  debug: function(){},
  log: function(){},
  group: function(){},
  dir: function(){},
  dirxml: function(){}
};

(function(){
  jck = {};

  ////////////////////// LOCATION TOOLS
  jck.location = {};


  /**
   * Watcher classe.
   */

  jck.location.Watcher = function(fieldID, keyDepth){
    this.fieldID  = fieldID;
    this.keyDepth = keyDepth;

    $('#'+fieldID).bind('keyup', this.keyUpWatcher);
    
    console.info(fieldID);
  };

  jck.location.watchers = [];

  jck.location.addWatcher = function(fieldID, keyDepth){
    self.watchers[fieldID] = new jck.location.Watcher(fieldID, keyDepth);
    console.info(self);
  };


  jck.location.Watcher.prototype = {
    keyUpWatcher: function(){
      var watcher = self.getWatcherForField($(this));
      if( $(this).val().length >= watcher.keyDepth ){

        // On détermine si l'adresse est un nom de ville
        // ou un Code Postal.

        $.ajax({
          url: '/frontend_dev.php/jck/api/location/getaddress.xml',
          success: function(){
            console.info("SUCCESS");
          },
          data: {
            address: $(this).val()
          },
          method: 'GET',
          error: function(){
            console.error('Query failed');
            console.info($(this));
          }
        });
      }
    }
  };

  jck.location.getWatcherForField = function( jField ) {
    return self.watchers[jField.attr('id')];
  }

  var self = jck.location;


})();
